package tx.bjdacp.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.org.rapid_framework.beanutils.BeanUtils;
import cn.org.rapid_framework.web.scope.Flash;

import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.ModelDriven;

import java.util.*;

import javacommon.base.*;
import javacommon.util.*;

import cn.org.rapid_framework.util.*;
import cn.org.rapid_framework.web.util.*;
import cn.org.rapid_framework.page.*;
import cn.org.rapid_framework.page.impl.*;

import tx.bjdacp.model.*;
import tx.bjdacp.dao.*;
import tx.bjdacp.service.*;
import tx.bjdacp.vo.query.*;


public class FileCatalogAction extends BaseStruts2Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null;

	//forward paths
	protected static final String QUERY_JSP = "/pages/FileCatalog/query.jsp";
	protected static final String LIST_JSP= "/pages/FileCatalog/list.jsp";
	protected static final String CREATE_JSP = "/pages/FileCatalog/create.jsp";
	protected static final String CREATE_LIST_JSP = "/pages/FileCatalog/create-list.jsp";
	protected static final String EDIT_JSP = "/pages/FileCatalog/edit.jsp";
	protected static final String SHOW_JSP = "/pages/FileCatalog/show.jsp";
	//redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/pages/FileCatalog/list.do";

	private FileCatalogManager fileCatalogManager;

	private Integer totalCatalogId;

	public Integer getTotalCatalogId() {
		return totalCatalogId;
	}

	public void setTotalCatalogId(Integer totalCatalogId) {
		this.totalCatalogId = totalCatalogId;
	}

	private FileCatalog fileCatalog;
	java.lang.Integer id = null;
	private String[] items;

	public void prepare() throws Exception {

		if (isNullOrEmptyString(id)) {
			fileCatalog = new FileCatalog();
		} else {
			fileCatalog = (FileCatalog)fileCatalogManager.getById(id);
		}
	}

	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setFileCatalogManager(FileCatalogManager manager) {
		this.fileCatalogManager = manager;
	}

	public Object getModel() {
		return fileCatalog;
	}

	public void setFileCatalogId(java.lang.Integer val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}

	private TotalCatalogManager totalCatalogManager;
	public void setTotalCatalogManager(TotalCatalogManager manager) {
		this.totalCatalogManager = manager;
	}


	/** 执行搜索 */
	public String list() {
		if (!isNullOrEmptyString(totalCatalogId)) {
			TotalCatalog totalCatalog = (TotalCatalog)totalCatalogManager.getById(totalCatalogId);
			getRequest().setAttribute("totalCatalog", totalCatalog);
		}

		FileCatalogQuery query = newQuery(FileCatalogQuery.class,DEFAULT_SORT_COLUMNS);

		Page page = fileCatalogManager.findPage(query);
		savePage(page,query);
		return LIST_JSP;
	}

	public String listExcel() {
		if (!isNullOrEmptyString(totalCatalogId)) {
			TotalCatalog totalCatalog = (TotalCatalog)totalCatalogManager.getById(totalCatalogId);
			getRequest().setAttribute("totalCatalog", totalCatalog);
		}

		FileCatalogQuery query = newQuery(FileCatalogQuery.class,DEFAULT_SORT_COLUMNS);
		query.setPageSize(10000);
		Page page = fileCatalogManager.findPage(query);
		savePage(page,query);
		return "/pages/FileCatalog/list-excel.jsp";
	}

	/** 查看对象*/
	public String show() {
		return SHOW_JSP;
	}

	/** 进入新增页面*/
	public String create() {
		return CREATE_JSP;
	}

	private Integer drugSellUnitId;

	public Integer getDrugSellUnitId() {
		return drugSellUnitId;
	}

	public void setDrugSellUnitId(Integer drugSellUnitId) {
		this.drugSellUnitId = drugSellUnitId;
	}
	private DrugSellUnitManager drugSellUnitManager;
	public void setDrugSellUnitManager(DrugSellUnitManager manager) {
		this.drugSellUnitManager = manager;
	}

	private DrugSellUnit drugSellUnit;
	public DrugSellUnit getDrugSellUnit() {
		return drugSellUnit;
	}

	public void setDrugSellUnit(DrugSellUnit drugSellUnit) {
		this.drugSellUnit = drugSellUnit;
	}
	private ArchiveCatalog archiveCatalog;

	public ArchiveCatalog getArchiveCatalog() {
		return archiveCatalog;
	}

	public void setArchiveCatalog(ArchiveCatalog archiveCatalog) {
		this.archiveCatalog = archiveCatalog;
	}

	private TotalCatalog totalCatalog;

	public TotalCatalog getTotalCatalog() {
		return totalCatalog;
	}

	public void setTotalCatalog(TotalCatalog totalCatalog) {
		this.totalCatalog = totalCatalog;
	}

	public String createList() {
		 drugSellUnit=drugSellUnitManager.getById(drugSellUnitId);
		 Set<ArchiveCatalog> acs=drugSellUnit.getArchiveCatalogs();
		 int maxSeqNum=0;
		 if(acs.size()>0){
			 for (ArchiveCatalog archiveCatalog : acs) {
				 if(archiveCatalog.getArchiveCatalogName()!=null){
					 this.archiveCatalog=archiveCatalog;
					 Set<TotalCatalog> tcls=this.archiveCatalog.getTotalCatalogs();
					 for (TotalCatalog totalCatalog : tcls) {
						if(totalCatalog.getSeqNum()>maxSeqNum){
							maxSeqNum=totalCatalog.getSeqNum();
						}
					}
					 break;
				 }
			}
		 }else{
			 this.archiveCatalog=new ArchiveCatalog();
			 this.archiveCatalog.setFileNumber(drugSellUnit.getFileNumber());
			 getRequest().setAttribute("archiveCatalog", this.archiveCatalog);
		 }
		 this.totalCatalog=new TotalCatalog();
		 this.totalCatalog.setSeqNum(maxSeqNum);
		 getRequest().setAttribute("totalCatalog", this.totalCatalog);
		return CREATE_LIST_JSP;
	}

	private ArchiveCatalogManager archiveCatalogManager;
	public void setArchiveCatalogManager(ArchiveCatalogManager manager) {
		this.archiveCatalogManager = manager;
	}

	public String saveList() {
		archiveCatalogManager.saveOrUpdate(archiveCatalog);

		this.totalCatalog.setArchiveCatalogId(archiveCatalog.getArchiveCatalogId());
		totalCatalogManager.saveOrUpdate(totalCatalog);

		for (FileCatalog fileCatalog : fileCatalogs) {
			if(isNullOrEmptyString(fileCatalog.getDutyPerson())&&isNullOrEmptyString(fileCatalog.getFileCatalogName())){
				continue;
			}
			fileCatalog.setTotalCatalogId(totalCatalog.getTotalCatalogId());
			fileCatalogManager.save(fileCatalog);
		}


		Flash.current().success(CREATED_SUCCESS); //存放在Flash中的数据,在下一次http请求中仍然可以读取数据,error()用于显示错误消息
		return LIST_ACTION;
	}

	/** 保存新增对象 */
	public String save() {
		fileCatalogManager.save(fileCatalog);
		Flash.current().success(CREATED_SUCCESS); //存放在Flash中的数据,在下一次http请求中仍然可以读取数据,error()用于显示错误消息
		return LIST_ACTION;
	}

	List<FileCatalog> fileCatalogs;

	public void setFileCatalogs(List<FileCatalog> fileCatalogs) {
		this.fileCatalogs = fileCatalogs;
	}

	public List<FileCatalog> getFileCatalogs() {
		return fileCatalogs;
	}



	/**进入更新页面*/
	public String edit() {
		return EDIT_JSP;
	}

	/**保存更新对象*/
	public String update() {
		fileCatalogManager.update(this.fileCatalog);
		Flash.current().success(UPDATE_SUCCESS);
		return LIST_ACTION;
	}

	/**删除对象*/
	public String delete() {
		for(int i = 0; i < items.length; i++) {
			Hashtable params = HttpUtils.parseQueryString(items[i]);
			java.lang.Integer id = new java.lang.Integer((String)params.get("fileCatalogId"));
			fileCatalogManager.removeById(id);
		}
		Flash.current().success(DELETE_SUCCESS);
		return LIST_ACTION;
	}

}
